package com.valuegroup.smarttodo;

import android.app.Application;
import android.content.Context;
import android.os.Build;
import android.text.TextUtils;
import android.widget.ImageView;

import com.alibaba.fastjson.JSON;
import com.bumptech.glide.Glide;
import com.github.piasy.biv.BigImageViewer;
import com.github.piasy.biv.loader.glide.GlideImageLoader;
import com.valuegroup.smarttodo.data.DBMaster;
import com.valuegroup.smarttodo.data.ServiceFactory;
import com.valuegroup.smarttodo.data.enums.GoalSortTypeEnum;
import com.valuegroup.smarttodo.data.enums.TaskSortTypeEnum;
import com.valuegroup.smarttodo.data.vo.AppCustomCfg;
import com.valuegroup.smarttodo.plugins.imgsel.ISNav;
import com.valuegroup.smarttodo.plugins.imgsel.common.ImageLoader;
import com.valuegroup.smarttodo.util.Cfg;

import androidx.annotation.RequiresApi;

public class SmartTodoApp extends Application {

    public static final String APP_CUSTOM_CFG = "AppCustomCfg";

    @Override
    public void onCreate() {
        System.out.println("SmartTodoApp onCreate");
        super.onCreate();

        // 初始化数据库
        DBMaster.init(this);

        // 图片预览
        BigImageViewer.initialize(GlideImageLoader.with(this));

        // 图片选择器
        ISNav.getInstance().init(new ImageLoader() {
            @Override
            public void displayImage(Context context, String path, ImageView imageView) {
                Glide.with(context).load(path).into(imageView);
            }
        });

        // 初始化业务数据
        initData();
    }

    @RequiresApi(api = Build.VERSION_CODES.N)
    private void initData() {
        String cfgStr = Cfg.read(this, APP_CUSTOM_CFG, "");
        ServiceFactory.getInstance().getTaskService().dispostData();
        if (TextUtils.isEmpty(cfgStr)) {

            System.out.println(">>>>>>>>>>>>>>> 首次运行");

            AppCustomCfg c = new AppCustomCfg();
            c.setFirstRun(false); // 记录首次运行

            c.setTaskHideFinish(false);
            c.setTaskSortDesc(false);
            c.setTaskSortType(TaskSortTypeEnum.NORMAL.getSortType());

            c.setGoalHideFinish(false);
            c.setGoalSortDesc(false);
            c.setGoalSortType(GoalSortTypeEnum.NORMAL.getSortType());

            c.setCurriculumHideFinish(false);
            c.setIdealityHideFinish(false);
            c.setHabitHideFinish(false);

            Cfg.write(this, APP_CUSTOM_CFG, JSON.toJSONString(c));
        }
    }


}
